package com.example.myapplication;

import static android.content.Context.MODE_PRIVATE;

import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import com.example.myapplication.adapter.ElementAdapter;
import com.example.myapplication.adapter.WeathAdapter;
import com.example.myapplication.utils.Request;
import com.example.myapplication.utils.UICache;
import org.json.JSONArray;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;


public class WeixinFragment extends Fragment {
    private View view;
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.tab1, container, false);
        new InitUI(view).execute();
        ((TextView)view.findViewById(R.id.city)).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent=new Intent(view.getContext(), Seach.class);
                startActivityForResult(intent,5656);
            }
        });
        return view;
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        //UICache.city=data.getSerializableExtra("city").toString();
        new InitUI(view).execute();
    }

    private  class InitUI extends AsyncTask<String, Integer, Document> {
        private View view;
        InitUI (View view){
            this.view=view;
        }
        @Override
        protected Document doInBackground(String... strings) {
            if (UICache.city==null){
                UICache.city= getActivity().getSharedPreferences("share", MODE_PRIVATE).getString("city","60480");
            }
            return Request.document("http://pc.weathercn.com/weather/week/"+UICache.city);
        }

        @Override
        protected void onPostExecute(Document d) {
            try {
                //today
                Element today = d.select("div[class=weather-real-time-wrap]").get(0);
                Elements limit = today.select("div[class=head-title-wrap g-clearfix g-light-blue]").first().select("span");
                if (limit!=null && limit.size()>0){
                    ((TextView)view.findViewById(R.id.limit)).setText(limit.first().text());
                }
                ((TextView)view.findViewById(R.id.city)).setText(today.select("div[class=head-title-wrap g-clearfix g-light-blue]").select("div").get(1).text());
                String qianqi = today.select("div[class=weather-icon-wrap]").select("span").first().text();
                ImageView pic = (ImageView) view.findViewById(R.id.ari_pic);
                if (qianqi.contains("小")){
                    pic.setImageResource(R.drawable.xiaoyu);
                }else if (qianqi.contains("中")){
                    pic.setImageResource(R.drawable.zhongyu);
                }else if (qianqi.contains("大")){
                    pic.setImageResource(R.drawable.dayu);
                }else if (qianqi.contains("阵")){
                    pic.setImageResource(R.drawable.zhenyu);
                }else if (qianqi.contains("阴")){
                    pic.setImageResource(R.drawable.yin);
                } else {
                    pic.setImageResource(R.drawable.qin);
                }
                ((TextView)view.findViewById(R.id.tianqi)).setText(qianqi);
                ((TextView)view.findViewById(R.id.temperature)).setText(today.select("div[class=weather-temp]").first().text()+"℃");
                Elements air = today.select("div[class=weather-detail]").select("span");
                ((TextView)view.findViewById(R.id.air)).setText(air.first().text()+"  "+air.get(1).text());
                ((TextView)view.findViewById(R.id.light)).setText(air.get(2).text()+"  "+air.get(3).text()+"  "+air.get(4).text());
                //everyDay
                d.select("div[class=forecast-slider]").select("a");
                //72hour
                StringBuilder b = new StringBuilder(d.select("div[id=weather-72forecast]").select("script").get(1).data());
                JSONArray weath = new JSONArray(b.substring(b.indexOf("[{\"hour\""), b.indexOf("}}]") + 3));

                RecyclerView recyclerView = (RecyclerView) view.findViewById(R.id.recycler_view_72hour);
                LinearLayoutManager layoutManager = new LinearLayoutManager(view.getContext(),LinearLayoutManager.HORIZONTAL,false);//禁止滑动 重写canScrollHorizontally返回false
                recyclerView.setLayoutManager(layoutManager);
                recyclerView.setAdapter(new WeathAdapter(weath));
                //15day
                RecyclerView recycler15day = (RecyclerView) view.findViewById(R.id.recycler_view_15day);
                recycler15day.setLayoutManager(new LinearLayoutManager(view.getContext(),LinearLayoutManager.HORIZONTAL,false));
                Elements e = d.select("div[class=forecast-slider]").select("a");
                recycler15day.setAdapter(new ElementAdapter(e));


                /*WebView webView = (WebView)view.findViewById(R.id.webpage);
                webView.loadUrl("http://pc.weathercn.com/weather/week/2333389/");
                //允许执行js
                WebSettings settings = webView.getSettings();
                settings.setJavaScriptEnabled(true);
                settings.setJavaScriptCanOpenWindowsAutomatically(true);

                webView.setWebViewClient(new WebViewClient() {
                    @Override
                    public void onPageFinished(WebView view, String url) {
                        //在页面加载完成时，调用js
                        webView.loadUrl(" javascript:function hideTitle() { document.getElementById('header').style.display='none'; } ");
                        //执行方法
                        webView.loadUrl("javascript:hideTitle();");
                        super.onPageFinished(view, url);
                    }
                });*/

            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }




}
